
//  JQFYMineViewController.m
//  JQFY
//
//  Created by apple on 2017/8/6.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYMineViewController.h"
#import "JQFYMyShowController.h"
#import "JQFYChatGroupController.h"
#import "JQFYSettingController.h"
#import "JQFYBaseNavigationController.h"
#import "JQFYUserInfoModel.h"

#import "JQFYLonginViewController.h"
#import "JQFYSettingUserInfoViewController.h"

@interface JQFYMineViewController () <UIScrollViewDelegate,JQFYSettingControllerDelegate>


@property (nonatomic,weak) UIImageView *iconImageView;           // 头像
@property (nonatomic,strong) UILabel *nickNameLabel;               // 昵称
@property (nonatomic,weak) UIButton *chatGroup;
@property (nonatomic,weak) UIButton *myShow;
@property (nonatomic,strong) ZJAnimationPopView *alert;
@property (nonatomic ,copy) NSString *sex;  //性别
@property (nonatomic,assign) BOOL isSetupUI;

@end

#define ChatGroupWidth 108 *SCAL
#define MyShowWidth     81  *SCAL
#define GroupHeight    12  *SCAL
@implementation JQFYMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//<<<<<<< HEAD
//=======
//    
//    [self setupUI];
//    [self setupNav];
//>>>>>>> master
}

-(void)viewWillAppear:(BOOL)animated{
    
    
    JQFYUserData* data = [JQFYUserData userDataInSandbox];
    if (!data.token) {
        JQFYLonginViewController *loginVc = [[JQFYLonginViewController alloc] init];
        [self presentViewController:loginVc animated:YES completion:^{
            
            //创建通知对象
            [NSNotification notificationWithName:@"didClickStartButton" object:self];
            //发布通知
            [[NSNotificationCenter defaultCenter] postNotificationName:@"didClickStartButton" object:self];
            
        }];
    }else{
        
        if (self.isSetupUI) {
            return;
        }
        self.isSetupUI = YES;
        [self loadUserData];
        [self setupUI];
        [self setupNav];
        
    }
    
}
- (void)loadUserData
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"isSave"]) {
        NSLog(@"teturn");
        return;
    }
    __weak typeof(self) weakSelf = self;
    [[JQFYUserInfoModel new] getUserInfo:^(NSDictionary *userInfo) {
        
        if ([userInfo[@"status"] isEqualToString:@"error"]) {
            
            weakSelf.iconImageView.image = [UIImage imageNamed:@"组5（合并）"];
            weakSelf.nickNameLabel.text = @"优秀青年";
            NSLog(@"error");
            //            weakSelf.sex = @"男";
        } else {
            NSLog(@"success");
            [weakSelf.iconImageView sd_setImageWithURL:[NSURL URLWithString:userInfo[@"msg"][@"head"]] placeholderImage:[UIImage imageNamed:@"组5（合并）"]];
            weakSelf.nickNameLabel.text = userInfo[@"msg"][@"nickname"];
            //            weakSelf.sex = userInfo[@"msg"][@"sex"];
            [[NSUserDefaults standardUserDefaults] setObject:userInfo[@"msg"][@"nickname"] forKey:@"nickName"];
            NSData *iconData = UIImagePNGRepresentation(weakSelf.iconImageView.image);
            [[NSUserDefaults standardUserDefaults] setObject:iconData forKey:@"iconData"];
        }
    } fail:nil];
}

#pragma mark - 自定义导航栏 -
- (void)setupNav
{
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    
    UIImageView *logoImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo1（合并）"]];
    [self.view addSubview:logoImage];
    [logoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_top).offset(55 *SCAL);
        make.left.equalTo(self.view).offset(15 *SCAL);
    }];
    
    UIButton *settingBtn = [UIButton new];
    settingBtn.alpha = 0.88;
    settingBtn.frame = CGRectMake(0, 0, 20 *SCAL, 20*SCAL);
    [settingBtn setBackgroundImage:[UIImage imageNamed:@"设置(1)"] forState:UIControlStateNormal];
    [settingBtn addTarget:self action:@selector(setting) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:settingBtn];
    
}

- (void)setupUI
{
    
    self.view.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"my_info_bg"].CGImage);
    // 头像
    UIImageView *iconImageView = [UIImageView new];
    iconImageView.userInteractionEnabled = YES;
    iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.iconImageView = iconImageView;
    
    // 切圆角
    iconImageView.layer.cornerRadius = 70*SCAL;
    iconImageView.layer.masksToBounds = YES;
    iconImageView.image = [UIImage imageNamed:@"组5（合并）"];
    [self.view addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(104*SCAL);
        make.width.height.mas_equalTo(140 *SCAL);
    }];
    // 点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconImageViewtap:)];
    [iconImageView addGestureRecognizer:tap];
    // 昵称
    self.nickNameLabel = [UILabel new];
    [self.view addSubview:self.nickNameLabel];
    self.nickNameLabel.font = [UIFont systemFontOfSize:20];
    [self.nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconImageView.mas_bottom).offset(6 *SCAL);
        make.centerX.equalTo(iconImageView);
    }];
    self.nickNameLabel.textColor = [UIColor whiteColor];
    self.nickNameLabel.text = @"优秀青年";
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"nickName"]) {
        self.nickNameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"nickName"];
    }
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"iconData"]) {
        iconImageView.image = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"iconData"]];
    }
    
    UIImageView *edmCommunityImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"edm_community"]];
    [self.view addSubview:edmCommunityImageView];
    [edmCommunityImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.nickNameLabel.mas_bottom).offset(28 *SCAL);
    }];
    
    UIImageView *edmBottomLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"—"]];
    
    [self.view addSubview:edmBottomLine];
    [edmBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(edmCommunityImageView.mas_bottom).offset(10 *SCAL);
    }];
    
    // 群聊
    UIButton *chatGroup = [UIButton new];
    [chatGroup setBackgroundImage:[UIImage imageNamed:@"GROUPCHAT"] forState:UIControlStateNormal];
    [self.view addSubview:chatGroup];
    [chatGroup addTarget:self action:@selector(chatGroup:) forControlEvents:UIControlEventTouchUpInside];
    
    [chatGroup mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(70*SCAL);
        make.top.equalTo(edmBottomLine.mas_bottom).offset(30 *SCAL);
    }];
    
    // 个人show
    UIButton *myShow = [UIButton new];
    
    [myShow setBackgroundImage:[UIImage imageNamed:@"MYSHOW"] forState:UIControlStateNormal];
    [self.view addSubview:myShow];
    [myShow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(chatGroup);
        make.right.equalTo(self.view).offset(-70 *SCAL);
    }];
    
    [myShow addTarget:self action:@selector(myShow:) forControlEvents:UIControlEventTouchUpInside];
    
    _myShow = myShow;
    _chatGroup = chatGroup;
    
    // VIP
    UIButton *vipBtn = [UIButton new];
    vipBtn.userInteractionEnabled = NO;
    [self.view addSubview:vipBtn];
    [vipBtn setBackgroundImage:[UIImage imageNamed:@"become_vip"] forState:UIControlStateNormal];
    [vipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(myShow.mas_bottom).offset(42 *SCAL);
    }];
    
    UIImageView *vipBottomLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"一"]];
    [self.view addSubview:vipBottomLine];
    [vipBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(vipBtn.mas_bottom).offset(20 *SCAL);
    }];
    
    UIImageView *readyImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ready"]];
    [self.view addSubview:readyImageView];
    [readyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(vipBottomLine.mas_bottom).offset(14 *SCAL);
    }];
}

#pragma mark - 点击切换头像 -
- (void)iconImageViewtap:(UITapGestureRecognizer *)tap
{
    JQFYSettingController *setting = [JQFYSettingController new];
    setting.delegate = self;
    setting.iconImage = self.iconImageView.image;
    setting.nickName = self.nickNameLabel.text;
    
    [self.navigationController pushViewController:setting animated:YES];
}

#pragma mark - 群聊 -
- (void)chatGroup:(UIButton *)btn
{
#warning 需要网络请求判断是否参加活动
    
    MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    hud.detailsLabelText = @"暂未开放~~~";
    [hud hide:YES afterDelay:1];
    
    return;
    
    JQFYChatGroupController *chatGroupVC = [JQFYChatGroupController new];
    [self.navigationController pushViewController:chatGroupVC animated:YES];
    
}

- (void)tap{
    [self.alert dismiss];
    
}

- (void)alertError:(NSString *)imageName
{
    UIImageView *alertError = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    alertError.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [alertError addGestureRecognizer:tap];
    
    self.alert = [[ZJAnimationPopView alloc] initWithCustomView:alertError popStyle:ZJAnimationPopStyleScale dismissStyle:ZJAnimationDismissStyleScale];
    self.alert.popBGAlpha = 0.6;
    self.alert.isClickBGDismiss = YES;
    [self.alert pop];
}
#pragma mark - myShow -
- (void)myShow:(UIButton *)btn
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"isBuy"]) {
        
        JQFYMyShowController *chatGroupVC = [JQFYMyShowController new];
        [self.navigationController pushViewController:chatGroupVC animated:YES];
        return;
    }
    [self alertError:@"X（合并）"];
}

#pragma mark - 设置 -
- (void)setting
{
    JQFYSettingController *setting = [JQFYSettingController new];
    setting.delegate = self;
    setting.iconImage = self.iconImageView.image;
    setting.nickName = self.nickNameLabel.text;
    //    setting.sex = self.sex;
    [self.navigationController pushViewController:setting animated:YES];
}

#pragma mark - 代理 -
- (void)settingVCChnageName:(JQFYSettingController *)controller andName:(NSString *)name icon:(UIImage *)icon
{
    self.nickNameLabel.text = name;
    self.iconImageView.image = icon;
}

#pragma mark - 移除通知 -
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

